<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="../assets/js/vue.js"></script>
    <title>V-for 案例</title>
</head>
<body>
    <h1>v-for指令用法</h1>
    <hr>
    <div id="app">
       <ul>
           <li v-for="item in sortItems">
                {{item}}
           </li>
       </ul>
       <ul>
        <!--index输出序号-->
        <li v-for="(student,num) in sortstudents">
             {{num+1}}:{{student.name}}--{{student.age}}
        </li>
    </ul>
    </div>
    <script type="text/javascript">
        var app=new Vue({
            el:'#app',
            data:{
                items:[20,23,18,65,32,19,5],//第一个简单数组
                students:[ //第二个复杂数组
                    {name:'heheda0',age:33},
                    {name:'heheda1',age:22},
                    {name:'heheda2',age:18},
                    {name:'heheda3',age:5}
                ]
            },
            computed:{  //排序
                sortItems:function(){ //重新定义一个名字，借助sort排序（sort排序根据首数字排序，有问题）
                    return this.items.sort(sortnumbr);//借助js自定义排序办法
                },
                sortstudents:function(){
                    return sortbykey(this.students,'age');
                }
            } 
        });
        function sortnumbr(a,b){ //自定义排序
            return a-b;
        }
        function sortbykey(arr,key){ //对象排序
            return arr.sort(function(a,b){
                var x=a[key];
                var y=b[key];
                return ((x<y)?1:((x>y)?1:0));
            })
        }
    </script>
</body>
</html>